package com.admin.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.admin.bean.BillingBean;
import com.admin.bean.ProductBean;
import com.admin.service.BillingService;
import com.admin.service.ProductService;
import com.admin.util.BaseUtil;
import com.admin.util.PageBean;

@Controller
@RequestMapping("/billing")
public class BillingAction {
	
	@Autowired private BillingService billingService;
	@Autowired private ProductService productService;
	
	@RequestMapping("/addUI")
	public String insertUI(HttpServletRequest request){
		int productId = BaseUtil.stringToInt(request.getParameter("productId"));
		List<ProductBean> productList = productService.findAllProduct();
		request.setAttribute("productId", productId);
		request.setAttribute("productList", productList);
		return "billingAdd";
	}
	
	@RequestMapping("/add")
	public ModelAndView insertBilling(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String billingName = request.getParameter("billingName");
		int fee = BaseUtil.stringToInt(request.getParameter("fee"));
		int spendLimit = BaseUtil.stringToInt(request.getParameter("spendLimit"));
		int billingType = BaseUtil.stringToInt(request.getParameter("billingType"));
		int days = BaseUtil.stringToInt(request.getParameter("days"));
		int tipType = BaseUtil.stringToInt(request.getParameter("tipType"));
		int periodHour = BaseUtil.stringToInt(request.getParameter("periodHour"));
		int confirmTimes = BaseUtil.stringToInt(request.getParameter("confirmTimes"),0);
		int userType = BaseUtil.stringToInt(request.getParameter("userType"));
		String tipMessage = request.getParameter("tipMessage");
		String memo = request.getParameter("memo");
		int productId = BaseUtil.stringToInt(request.getParameter("productId"));
		String productName = productService.findProductById(productId).getProductName();
		
		BillingBean billing = new BillingBean();
		billing.setBillingName(billingName);
		billing.setFee(fee);
		billing.setSpendLimit(spendLimit);
		billing.setBillingType(billingType);
		billing.setDays(days);
		billing.setTipType(tipType);
		billing.setPeriodHour(periodHour);
		billing.setConfirmTimes(confirmTimes);
		billing.setUserType(userType);
		billing.setTipMessage(tipMessage);
		billing.setMemo(memo);
		billing.setProductId(productId);
		billing.setProductName(productName);
		
		int result = billingService.insertBilling(billing);
		
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?");
		int productId = BaseUtil.stringToInt(request.getParameter("productId"));
		if(productId!=0){
			url.append("productId="+productId);
		}
		int userType = BaseUtil.stringToInt(request.getParameter("userType"),-1);
		if(userType!=-1){
			url.append(productId!=0?"&":"");
			url.append("userType="+userType);
		}
		int totalCount = billingService.findCountByCondition(productId, userType);
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<BillingBean> pageBean = new PageBean<BillingBean>(totalCount, currentPage);
		List<BillingBean> billingList = billingService.findBillingByCondition(productId, userType, pageBean.getStart(), pageBean.getPageSize());
		pageBean.setResult(billingList);
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		
		List<ProductBean> productList = productService.findAllProduct();
		request.setAttribute("productId", productId);
		request.setAttribute("userType", userType);
		request.setAttribute("productList", productList);
		
		return "billingList";
		
	}
	
	@RequestMapping("/del/{billingId}")
	public ModelAndView del(@PathVariable int billingId,HttpServletResponse response) throws IOException{
		int result = billingService.delBilling(billingId);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/view")
	public String show(HttpServletRequest request){
		int billingId = BaseUtil.stringToInt(request.getParameter("billingId"));
		BillingBean bill = billingService.findBillingById(billingId);
		request.setAttribute("billing", bill);
		List<ProductBean> productList = productService.findAllProduct();
		request.setAttribute("productList", productList);
		return "billingView";
	}
	
	@RequestMapping("/update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int billingId = BaseUtil.stringToInt(request.getParameter("billingId"));
		String billingName = request.getParameter("billingName");
		int fee = BaseUtil.stringToInt(request.getParameter("fee"));
		int spendLimit = BaseUtil.stringToInt(request.getParameter("spendLimit"));
		int billingType = BaseUtil.stringToInt(request.getParameter("billingType"));
		int days = BaseUtil.stringToInt(request.getParameter("days"));
		int tipType = BaseUtil.stringToInt(request.getParameter("tipType"));
		int periodHour = BaseUtil.stringToInt(request.getParameter("periodHour"));
		int confirmTimes = BaseUtil.stringToInt(request.getParameter("confirmTimes"),0);
		int userType = BaseUtil.stringToInt(request.getParameter("userType"));
		String tipMessage = request.getParameter("tipMessage");
		String memo = request.getParameter("memo");
		int productId = BaseUtil.stringToInt(request.getParameter("productId"));
		String productName = productService.findProductById(productId).getProductName();
		
		BillingBean billing = new BillingBean();
		billing.setBillingId(billingId);
		billing.setBillingName(billingName);
		billing.setFee(fee);
		billing.setSpendLimit(spendLimit);
		billing.setBillingType(billingType);
		billing.setDays(days);
		billing.setTipType(tipType);
		billing.setPeriodHour(periodHour);
		billing.setConfirmTimes(confirmTimes);
		billing.setUserType(userType);
		billing.setTipMessage(tipMessage);
		billing.setMemo(memo);
		billing.setProductId(productId);
		billing.setProductName(productName);
		
		int result = billingService.updateBilling(billing);
		
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
}
